package hos.demo

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import hos.constants.Api
import hos.demo.databinding.ActivityNormalBinding
import hos.http.Http
import hos.http.HttpCallback
import hos.http.HttpConfig
import hos.http.HttpRequest
import hos.http.converter.HttpConvert
import hos.okhttp.convert.HttpResponseMapListConvert
import java.util.UUID

class NormalActivity : AppCompatActivity() {
    private val activityMainBinding by lazy { ActivityNormalBinding.inflate(layoutInflater) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(activityMainBinding.root)
        activityMainBinding.btn1.setOnClickListener {
            Http.get(Api.TEXT)
                .enqueue<String>(object : HttpCallback<String>() {
                    override fun onSuccess(request: HttpRequest<*>?, response: String?) {
                        Log.i("Http", "Text" + response.toString());
                        Toast.makeText(this@NormalActivity, response.toString(), Toast.LENGTH_SHORT)
                            .show()
                    }

                    override fun onFailed(throwable: Throwable?) {
                        throwable?.printStackTrace()
                    }

                })
        }
        activityMainBinding.btn2.setOnClickListener {
            HttpConfig.runIo {
                val result = Http.get(Api.DELAY).dialog(true, this).asString()
                Log.d("TAG", "delay: $result")
                runOnUiThread {
                    Toast.makeText(this@NormalActivity, result.toString(), Toast.LENGTH_SHORT)
                        .show()
                }
            }
        }
        activityMainBinding.btn3.setOnClickListener {
            Http.post(Api.GAME)
                .param("keyword", "张三")
                .convert(HttpResponseMapListConvert() as HttpConvert<*>)
                .enqueue<MutableMap<String, Any>>(object : HttpCallback<MutableMap<String, Any>>() {
                    override fun onSuccess(
                        request: HttpRequest<*>?,
                        response: MutableMap<String, Any>?
                    ) {
                        Log.i("Http", "Game" + response.toString());
                        Toast.makeText(this@NormalActivity, response.toString(), Toast.LENGTH_SHORT)
                            .show()
                    }

                    override fun onFailed(throwable: Throwable?) {
                        throwable?.printStackTrace()
                    }

                })
        }
        activityMainBinding.btn4.setOnClickListener {
            Http.get(Api.TEXT)
                .enqueue<String>(object : HttpCallback<String>() {
                    override fun onSuccess(request: HttpRequest<*>?, response: String?) {
                        Log.i("Http", "Time" + response.toString());
                        Toast.makeText(this@NormalActivity, response.toString(), Toast.LENGTH_SHORT)
                            .show()
                    }

                    override fun onFailed(throwable: Throwable?) {
                        throwable?.printStackTrace()
                    }

                })
        }
        activityMainBinding.btn5.setOnClickListener {
            Http.get(Api.ARRAY)
                .convert(HttpResponseMapListConvert() as HttpConvert<*>)
                .enqueue<MutableList<MutableMap<String, Any>>>(object :
                    HttpCallback<MutableList<MutableMap<String, Any>>>() {
                    override fun onSuccess(
                        request: HttpRequest<*>?,
                        response: MutableList<MutableMap<String, Any>>?
                    ) {
                        Log.i("Http", "array" + response.toString());
                        Toast.makeText(this@NormalActivity, response.toString(), Toast.LENGTH_SHORT)
                            .show()
                    }

                    override fun onFailed(throwable: Throwable?) {
                        throwable?.printStackTrace()
                    }

                })
        }
        activityMainBinding.btn6.setOnClickListener {
            Http.get(Api.CONFIG)
                .convert(HttpResponseMapListConvert() as HttpConvert<*>)
                .enqueue<MutableMap<String, Any>>(object : HttpCallback<MutableMap<String, Any>>() {
                    override fun onSuccess(
                        request: HttpRequest<*>?,
                        response: MutableMap<String, Any>?
                    ) {
                        Log.i("Http", "config" + response.toString());
                        Toast.makeText(this@NormalActivity, response.toString(), Toast.LENGTH_SHORT)
                            .show()
                    }

                    override fun onFailed(throwable: Throwable?) {
                        throwable?.printStackTrace()
                    }

                })
        }
        activityMainBinding.btn7.setOnClickListener {
            Http.get(Api.Token)
                .enqueue<String>(object : HttpCallback<String>() {
                    override fun onSuccess(request: HttpRequest<*>?, response: String?) {
                        Log.i("Http", "token" + response.toString());
                        Toast.makeText(this@NormalActivity, response.toString(), Toast.LENGTH_SHORT)
                            .show()
                    }

                    override fun onFailed(throwable: Throwable?) {
                        throwable?.printStackTrace()
                    }

                })

        }
        activityMainBinding.btn8.setOnClickListener {
            Http.post(Api.USER_INFO)
                .param("token", "${System.currentTimeMillis()} " + UUID.randomUUID())
                .convert(HttpResponseMapListConvert() as HttpConvert<*>)
                .enqueue<MutableMap<String, Any>>(object : HttpCallback<MutableMap<String, Any>>() {
                    override fun onSuccess(
                        request: HttpRequest<*>?,
                        response: MutableMap<String, Any>?
                    ) {
                        Log.i("Http", "getUserInfo" + response.toString());
                        Toast.makeText(this@NormalActivity, response.toString(), Toast.LENGTH_SHORT)
                            .show()
                    }

                    override fun onFailed(throwable: Throwable?) {
                        throwable?.printStackTrace()
                    }

                })
        }
        activityMainBinding.btn9.setOnClickListener {
            val list = mutableListOf<MutableMap<String, Any>>()
            for (i in 0..10) {
                list.add(
                    mutableMapOf(
                        Pair("name", "张三"),
                        Pair("value", "历史"),
                        Pair("info", "详情信息"),
                        Pair("uuid", "${System.currentTimeMillis()}" + UUID.randomUUID())
                    )
                )
            }
            Http.post(Api.REQUEST)
                .data(list)
                .convert(HttpResponseMapListConvert() as HttpConvert<*>)
                .enqueue<MutableList<MutableMap<String, Any>>>(object :
                    HttpCallback<MutableList<MutableMap<String, Any>>>() {
                    override fun onSuccess(
                        request: HttpRequest<*>?,
                        response: MutableList<MutableMap<String, Any>>?
                    ) {
                        Log.i("Http", "request" + response.toString());
                        Toast.makeText(this@NormalActivity, response.toString(), Toast.LENGTH_SHORT)
                            .show()
                    }

                    override fun onFailed(throwable: Throwable?) {
                        throwable?.printStackTrace()
                    }

                })
        }
        activityMainBinding.btn10.setOnClickListener {
            Http.post(Api.REQUEST)
                .data(
                    mapOf(
                        Pair("name", "张三"),
                        Pair("value", "历史"),
                        Pair("info", "详情信息"),
                        Pair("uuid", "${System.currentTimeMillis()} " + UUID.randomUUID())
                    )
                )
                .convert(HttpResponseMapListConvert() as HttpConvert<*>)
                .enqueue<MutableMap<String, Any>>(object : HttpCallback<MutableMap<String, Any>>() {
                    override fun onSuccess(
                        request: HttpRequest<*>?,
                        response: MutableMap<String, Any>?
                    ) {
                        Log.i("Http", "request" + response.toString());
                        Toast.makeText(this@NormalActivity, response.toString(), Toast.LENGTH_SHORT)
                            .show()
                    }

                    override fun onFailed(throwable: Throwable?) {
                        throwable?.printStackTrace()
                    }

                })
        }
    }
}